package deal;

import com.fasterxml.jackson.databind.ObjectMapper;
import utils.SplitFileName;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SplitJsonFileByCKSJ {

    public static void run(){
        String inputFileName1="test/outSZ.json";
        String outPutFileName1=SplitFileName.sf(inputFileName1);
        SplitJsonFile(inputFileName1,outPutFileName1,"RKSJ");
        String inputFileName2="test/inSZ.json";
        String outPutFileName2=SplitFileName.sf(inputFileName2);
        SplitJsonFile(inputFileName2,outPutFileName2,"CKSJ");
        System.out.println("按照时间分割数据集成功！！");
    }

    public static void SplitJsonFile(String inputFileName,String outPutFileName,String type){
        String inputJsonFile = inputFileName;
        String outputJson1File = outPutFileName+"1.json";
        String outputJson2File = outPutFileName+"2.json";

        try {
            List<Map<String, String>> jsonData = readDataFromFile(inputJsonFile);
            splitAndWrite(jsonData, outputJson1File, outputJson2File,type);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static List<Map<String, String>> readDataFromFile(String filePath) throws IOException {
        List<Map<String, String>> jsonData = new ArrayList<>();

        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            ObjectMapper objectMapper = new ObjectMapper();
            String line;
            while ((line = reader.readLine()) != null) {
                Map<String, String> rowData = objectMapper.readValue(line, Map.class);
                jsonData.add(rowData);
            }
        }

        return jsonData;
    }

    private static void splitAndWrite(List<Map<String, String>> jsonData, String outputJson1File, String outputJson2File,String type)
            throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        List<Map<String, String>> json1 = new ArrayList<>();
        List<Map<String, String>> json2 = new ArrayList<>();

        // 获取当前日期和时间
        LocalDateTime now = LocalDateTime.now();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("2020-12-22 "+"HH:mm");
        // 将当前日期时间格式化为字符串
        String systemTime = now.format(formatter);


        for (Map<String, String> rowData : jsonData) {
            String cksj = rowData.get(type);

            // 比较时间
            if (cksj.compareTo(systemTime) <= 0) {
                json1.add(rowData);
            } else {
                json2.add(rowData);
            }
        }

        writeDataToFile(json1, outputJson1File);
        writeDataToFile(json2, outputJson2File);

        System.out.println("分割完成！");
    }

    private static void writeDataToFile(List<Map<String, String>> data, String outputFile) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();

        try (FileWriter jsonWriter = new FileWriter(outputFile)) {
            for (Map<String, String> rowData : data) {
                String jsonLine = objectMapper.writeValueAsString(rowData);
                jsonWriter.write(jsonLine);
                jsonWriter.write(System.lineSeparator());
            }
        }
    }
}
